package com.yh.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.yh.javabean.Product;
import com.yh.javabean.Tarr;
import com.yh.service.ProductService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.context.SecurityContext;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@Controller
@RequestMapping("/product")
public class ProductController {




    /*
    产品管理
    */
    @Autowired
    ProductService productService;


    @RequestMapping("/name")
    public String getUsername(HttpServletRequest request){
        SecurityContext context = SecurityContextHolder.getContext();
        UserDetails userDetails= (UserDetails)context.getAuthentication().getPrincipal();
        request.getSession().setAttribute("username",userDetails.getUsername());
        return "main";
    }

    @RequestMapping("/findAll")
    public String findAll(@RequestParam(value = "page", required = false, defaultValue = "1") int page,
                          @RequestParam(value = "limit", required = false, defaultValue = "5") int limit,
                          @RequestParam(value = "productName", required = false, defaultValue = "") String productName,
                          HttpServletRequest request) {
        PageHelper.startPage(page, limit);
        //List<Product> products = productService.findAll();
        List<Product> products = productService.findAll(productName);
        PageInfo<Product> pageInfo = new PageInfo<>(products);
        //request.setAttribute("products",products);
        request.setAttribute("pageInfo", pageInfo);

        request.setAttribute("value", productName);
        return "/product/product-list";
    }

  //查询角色详情
    @RequestMapping("/show")
    public String findAll(int id, HttpServletRequest request) {
        Product product = productService.findById(id);
        request.setAttribute("product", product);
        return "/product/product-show";
    }

    @RequestMapping("/editById")
    public String editById(int id, HttpServletRequest request) {
        Product product = productService.findById(id);
        request.setAttribute("product", product);
        return "/product/product-edit";
    }

    @RequestMapping("/edit")
    public String edit(Product product) {
        productService.editProduct(product);

        return "redirect:findAll";
    }

    @RequestMapping("/add")
    public String add(Product product) {
        productService.addProduct(product);
        return "redirect:findAll";
    }

    @RequestMapping("/delete")
    public String delete(int id) {
        productService.delete(id);
        return "redirect:findAll";
    }

    @RequestMapping("/deleteIds")
    @ResponseBody
    public String deleteIds(@RequestBody List<Integer> ids) {
        productService.deleteMore(ids);
        return "/product/findAll";
    }

    /*========================================================================================*/
   /*
    产品管理
    */
    @RequestMapping("/json")
    @ResponseBody
    public PageInfo<Product> json(@RequestParam(value = "page", required = false, defaultValue = "1") int page,
                                  @RequestParam(value = "limit", required = false, defaultValue = "5") int limit,
                                  @RequestParam(value = "productName", required = false, defaultValue = "") String productName,
                                  HttpServletRequest request) {
        PageHelper.startPage(page, limit);
        List<Product> products = productService.findAll(productName);
        PageInfo<Product> pageInfo = new PageInfo<>(products);
        return pageInfo;
    }

    @RequestMapping(value = "/editJson", method = RequestMethod.POST)
    public String editJson(@RequestBody Product product) {
        productService.editProduct(product);
        return "redirect:json";
    }

    @RequestMapping(value = "/addJson", method = RequestMethod.POST)
    public String addJson(@RequestBody Product product) {
        productService.addProduct(product);
        return "redirect:json";
    }

    @RequestMapping(value = "/deleteJson", method = RequestMethod.GET)
    public String deleteJson(int id) {
        productService.delete(id);
        return "redirect:json";
    }

    @RequestMapping(value = "/deleteIdsjson")
    public String deleteIdsjson(@RequestBody Tarr t) {

        productService.deleteMore(t.getIds());
        return "redirect:json";
    }

}
